package DataBase;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class GetComicEpisode {

	private static Connection connection = null;

	public static Connection getCon() {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			connection = DriverManager
					.getConnection(
							"jdbc:mysql://localhost:3306/Comic?useUnicode=true&characterEncoding=utf8",
							"root", "");
			if (connection.isClosed())
				System.out.println("the connection is still closed");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return connection;
	}
	
	private static void getEpisode(){
		
		String sel = "select id,comicName,link from comicInformation";
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			preparedStatement = connection.prepareStatement(sel);
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				long id = resultSet.getLong(1);
				String comicName = resultSet.getString(2);
				String link = resultSet.getString(3);
				System.out.println(id+":"+comicName);
				getContent(id,comicName,link);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				resultSet.close();
				preparedStatement.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	public static void getContent(long id , String comicName,String url) {
		try {
			Document doc = Jsoup.connect(url).data("jquery", "java")
					.userAgent("Mozilla").cookie("auth", "token")
					.timeout(50000).get();
			String allHtml = doc.toString();
			doc = Jsoup.parse(allHtml);
			Elements elements = doc.getElementsByClass("ptab"); 
			for (Element element : elements) {
				String source = element.attr("id");
				dealWith(element.toString(),id,source,comicName);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static void dealWith(String content,long id,String source,String comicName) {
		
		Document doc = Jsoup.parse(content);
		Elements elements = doc.getElementsByTag("li");
		
		for (Element element : elements) {
			storeInDB(element.text(),element.getElementsByTag("a").attr("href"),id,source,comicName);
		}
		
	}
	
	private static void storeInDB(String episode,String link,long id,String source,String comicName) {
//		System.out.println(episode);
//		System.out.println(link);
//		System.out.println(id);
//		System.out.println(source);
		String ins = "insert into comicEpisode value(?,?,?,?,?)";
		PreparedStatement preparedStatement = null;
		try {
			 preparedStatement = connection.prepareStatement(ins);
			 preparedStatement.setLong(1, id);
			 preparedStatement.setString(2, comicName);
			 preparedStatement.setString(3, episode);
			 preparedStatement.setString(4, source);
			 preparedStatement.setString(5, link);
			 preparedStatement.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				preparedStatement.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
		getCon();
		getEpisode();
	}

}
